<%@ page contentType="text/html;charset=utf-8" language="java"
	pageEncoding="UTF-8" import="java.util.Date"%>
<!DOCTYPE html>
<html lang="en">
  <head>
     <%@include file="/WEB-INF/jsp/common/style.jsp"%>
  </head>
  <body>
    <div id="wrap">
    <input type="hidden" id="categoryId" value="${model.productCategory.id}"> 
   <div class="main_container" id="forms_page">
     	<div class="row-fluid">
          <div class="widget widget-padding span12">
        
              <div class="widget-forms clearfix"  style="padding-top:15px">
              	 <div class="form-horizontal" id="content">
              		<div class="control-group">
	                    <label class="control-label"><font color="red">*</font>分级级别:</label>
	                      <div class="controls">
	                        <select  id="categoryLevel" class="w300"  onchange="selectlevel()">
	                           	<option value="" <c:if test="${model.productCategory.categoryLevel==null}">selected="selected"</c:if>>请选择</option>
						   		<option value="1" <c:if test="${model.productCategory.categoryLevel==1}">selected="selected"</c:if>>一级分类</option>
						   		<option value="2" <c:if test="${model.productCategory.categoryLevel==2}">selected="selected"</c:if>>二级分类</option>
						   		<option value="3" <c:if test="${model.productCategory.categoryLevel==3}">selected="selected"</c:if>>三级分类</option>
						   		
	                        </select>
	                     </div>
	                  </div>
              	 
              	  <div class="control-group">
                    <label class="control-label" ><font color="red">*</font>上级分类:</label>
                    <div class="controls">
                     	 <select  id="categoryLevel1" class="w300" onchange="selectcategory1()">
	                           	<option    value="" <c:if test="${model.category1Id==null}">selected="selected"</c:if>>请一级分类</option>						   			
						   		<c:forEach items="${model.categorys1}" var="lo" varStatus="itemStatus">
						               <option value="${lo.id}" <c:if test="${model.category1Id==lo.id}">selected="selected"</c:if>>${lo.categoryName}</option>
						        </c:forEach>  
						   		
	                      </select>
	                       <select  id="categoryLevel2" class="w300">
	                       		<option    value="" <c:if test="${model.category2Id==null}">selected="selected"</c:if>>请二级级分类</option>	
	                    		<c:forEach items="${model.categorys2}" var="lo" varStatus="itemStatus">
						               <option value="${lo.id}" <c:if test="${model.category2Id==lo.id}">selected="selected"</c:if>>${lo.categoryName}</option>
						        </c:forEach>  
	                      </select>
                    </div>
                  </div>
                 
                 <div class="control-group">
                    <label class="control-label"><font color="red">*</font>分类名称:</label>
                    <div class="controls">
                     <input name="categoryName" id="categoryName"  maxlength="100"   type="text" class="w300 tip"value="${model.productCategory.categoryName}"   />                   
                    </div>
                  </div>
                  
                  <div class="control-group">
                    <label class="control-label"><font color="red">*</font>是否停用:</label>
                    <div class="controls">
                     <label class="radio">
                        <input name="status"  type="radio" 
                        <c:if test="${model.productCategory.status==1}">checked="checked"</c:if>  
                        value="1">
                        	否
                      </label>
                      <label class="radio">
                        <input name="status"  type="radio" 
                        <c:if test="${model.productCategory.status==0}">checked="checked"</c:if>  
                        value="0">
                     		是
                      </label>
                    </div>
                  </div>
					
					                  
              	  <div class="control-group">
                    <label class="control-label" >备注:</label>
                    <div class="controls">
                      <textarea class="w300" id="remark" rows="5" style="height:100px;" >${model.productCategory.remark}</textarea>                      
                     	     
                    </div>
                  </div>

                                                                  
                  </div>
                </div>
            </div>        
          </div>
          <div class="row-fluid">
          <div class="footbtn"> 
          	 <button class="btn btn-primary" type="submit"  onclick="save()">保存</button>
               <button class="btn" type="button" onclick="backIndex()">取消</button>
               </div>
          </div>
          </div>
      </div>    
  </body>
	<script type="text/javascript">
	$(function() {
		var categoryId=$("#categoryId").val();
		if(categoryId==''){
			//新增
			$("#categoryLevel1").hide();
			$("#categoryLevel2").hide();
		}else{
			selectlevel();
		}
	 });
	function selectlevel(){
		var level=$("#categoryLevel").val();
		//alert(level);
		if(level=='' || level =='1'){
			$("#categoryLevel1").hide();
			$("#categoryLevel2").hide();
		}
		if(level==2){
			$("#categoryLevel1").show();
			$("#categoryLevel2").hide();
		}
		if(level==3){
			$("#categoryLevel1").show();
			$("#categoryLevel2").show();
		}
	}
	function selectcategory1(){
		var categoryId=$("#categoryLevel1").val();
		if(null==categoryId || categoryId==''){
			return;
		}
        var url="${pageContext.request.contextPath}/set/category/getCategoryInfoByParentId";
        $.ajax({
            type: "POST",
            url: url,
            data: "parentId=" + categoryId,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            async: true, // 同步  true 异步。默认是异步
            dataType: 'json',
            error: function (datas) {              
            },
            success: function (data) {
                document.getElementById("categoryLevel2").options.length = 0;
                var options = "<option value='0' >请选二级分类</options>";
                for (var i = 0 ; i < data.length; i++ ) {
                    options += "<option value='" + data[i].id + "'>" +data[i].categoryName  + "</options>";
                }
                $(options).appendTo($("#categoryLevel2"));
            }
        });
	    
	}
	</script>
  <script type="text/javascript">
  function save(){
		var postData="categoryId=" +$('#categoryId').val() ;
		
		
		var categoryLevel=$('#categoryLevel').val();
		if(categoryLevel==''){
			layer.alert('请选择分级级别');
	    	return;
		}
		postData=postData + "&categoryLevel=" + categoryLevel;
		
		if(categoryLevel=='2' || categoryLevel =='3'){
			var categoryLevel1=$('#categoryLevel1').val();
			if(categoryLevel1==''){
				layer.alert('请选择一级分类');
		    	return;
			}
			
		  if(categoryLevel=='2'){
			  var categoryLevel1=$('#categoryLevel1').val();
				if(categoryLevel1==''){
					layer.alert('请选择一级分类');
			    	return;
				}
				postData=postData + "&parentId=" +categoryLevel1;
		   }
		   if(categoryLevel =='3'){
				var categoryLevel2=$('#categoryLevel2').val();
				if(categoryLevel2==''){
					layer.alert('请选择二级分类');
			    	return;
				}
				postData=postData + "&parentId=" +categoryLevel2;
			}
		}
		if(categoryLevel=='1'){
			postData=postData + "&parentId=0";
		}
		
		var categoryName=$('#categoryName').val();
		if(categoryLevel==''){
			layer.alert('请输入分类名称');
	    	return;
		}
		postData=postData + "&categoryName=" + encodeURIComponent(categoryName);
		
		
		var status= $("input[name='status']:checked").val();
		if(status==undefined){
			layer.alert('请选择是否停用');
	    	return;
		}
		postData=postData + "&status=" + status;
		
		postData=postData + "&remark=" + encodeURIComponent($('#remark').val());
		
	    layer.load(1, {shade: [0.5,'#fff']});
	   //alert(postData);
	    $.ajax({
	        type: "POST",
	        url: "save",
	        data: postData,
	        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
	        async: true, // 同步  true 异步。默认是异步
	        dataType: 'json',
	        error: function(resultdata) {
	        	layer.closeAll();
	        },
	        success: function(resultdata) {
	        	layer.closeAll();
	            if(resultdata.returnCode=="0000"){               	
	            	layer.alert('操作成功',{icon: 1}, function(){
	            	 	 parent.editReturn();
	            		//layer.closeAll(); 
	            		 //layer.closeAll();
	            		//window.location.href="${pageContext.request.contextPath}/distrbutor/inventory/index";
	            	});
	            }else{
	            	layer.alert(resultdata.returnMessage);
	            }
	        }
	    });
	}
  function backIndex(){	  
 	 parent.editReturn();
  }
	</script>
</html>
    